/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.dt.provider.utils;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.GspProject;
import com.inspur.edp.lcm.metadata.api.entity.MetadataProject;
import com.inspur.edp.lcm.metadata.api.service.GspProjectService;
import com.inspur.edp.lcm.metadata.api.service.MetadataProjectService;
import com.inspur.edp.lcm.metadata.api.service.MetadataService;
import com.inspur.edp.lcm.metadata.api.service.RefCommonService;
import io.iec.edp.caf.businessobject.api.entity.DevBasicBoInfo;
import io.iec.edp.caf.businessobject.api.service.DevBasicInfoService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.io.IOException;

/** 功能描述: @ClassName: MetadataUtils @Author: Fynn Qi @Date: 2020/9/1 14:15 @Version: V1.0 */
public class MetadataUtils {

  public static GspMetadata getDtVoMetadata(String resourceId) {
    if (StringUtils.isBlank(resourceId)) {
      throw new RuntimeException("外部服务获取解析的资源ID为空");
    }
    if(resourceId.contains("##")){
      String[] temps=resourceId.split("##");
      MetadataService service = SpringBeanUtils.getBean(MetadataService.class);
      return service.getRefMetadata(temps[1],temps[0]);
    }
    else{
      RefCommonService service = SpringBeanUtils.getBean(RefCommonService.class);
      return service.getRefMetadata(resourceId);
    }
  }

  public static MetadataProject getMetadataProject(String path) {
    MetadataProjectService service = SpringBeanUtils.getBean(MetadataProjectService.class);
    if (service == null) {
      throw new RuntimeException("获取MetadataProjectService Bean为空");
    }
    MetadataProject projectInfo = service.getMetadataProjInfo(path);
    if (projectInfo == null) {
      throw new RuntimeException(String.format("根据相对路径【%s】获取元数据工程信息为空！", path));
    }
    return projectInfo;
  }

  public static GspProject getProjectInfo(GspMetadata metadata) {
    if (StringUtils.isBlank(metadata.getRelativePath())) {
      throw new RuntimeException(
          String.format(
              "元数据【%s(ID:%s)】的相对路径为空，无法获取工程信息！",
              metadata.getHeader().getCode(), metadata.getHeader().getId()));
    }
    GspProjectService service = SpringBeanUtils.getBean(GspProjectService.class);
    if (service == null) {
      throw new RuntimeException("获取MetadataProjectService Bean为空");
    }
    GspProject projectInfo = null;
    try {
      projectInfo = service.getGspProjectInfo(metadata.getRelativePath());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    if (projectInfo == null) {
      throw new RuntimeException(
          String.format(
              "元数据【%s(ID:%s)】根据相对路径【%s】获取工程信息为空！",
              metadata.getHeader().getCode(),
              metadata.getHeader().getId(),
              metadata.getRelativePath()));
    }
    return projectInfo;
  }

  public static DevBasicBoInfo getDevBasicBoInfo(String bizId) {
    DevBasicInfoService devBasicInfoService = SpringBeanUtils.getBean(DevBasicInfoService.class);
    DevBasicBoInfo devBasicBoInfo = devBasicInfoService.getDevBasicBoInfo(bizId);
    Assert.notNull(devBasicBoInfo, String.format("根据业务对象ID[%s]获取的DevBasicBoInfo为空", bizId));
    if (StringUtils.isBlank(devBasicBoInfo.getAppCode())) {
      throw new RuntimeException(String.format("根据业务对象ID[%s]获取的DevBasicBoInfo为的AppCode为空", bizId));
    }
    if (StringUtils.isBlank(devBasicBoInfo.getSuCode())) {
      throw new RuntimeException(String.format("根据业务对象ID[%s]获取的DevBasicBoInfo为的SuCode为空", bizId));
    }
    return devBasicBoInfo;
  }
}
